package io.github.bloquesoft.entity.clazz.value;

import io.github.bloquesoft.entity.definition.EntityDefinition;
import io.github.bloquesoft.entity.clazz.ClassEntityDefinition;
import io.github.bloquesoft.entity.clazz.ClassListPropertyDefinition;
import io.github.bloquesoft.entity.definition.ListPropertyDefinition;
import io.github.bloquesoft.entity.value.EntityValue;
import io.github.bloquesoft.entity.value.ListPropertyValue;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class ObjectListPropertyValue implements ListPropertyValue {

    private final ClassListPropertyDefinition classEntityListProperty;

    private final ObjectEntityValue parent;

    public ObjectListPropertyValue(ClassListPropertyDefinition classEntityListProperty, ObjectEntityValue parent) {
        this.classEntityListProperty = classEntityListProperty;
        this.parent = parent;
    }

    @Override
    public Object getValue() {
        Object objectValue = this.parent.getValue();
        if (objectValue == null) {
            return null;
        }
        Field field = this.classEntityListProperty.getField();
        if (!field.isAccessible()) {
            field.setAccessible(true);
        }
        try {
            return field.get(objectValue);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Boolean isNullValue() {
        return this.getValue() == null;
    }

    @Override
    public String getPropertyId() {
        return classEntityListProperty.getId();
    }

    @Override
    public ListPropertyDefinition getListPropertyDefinition() {
        return this.classEntityListProperty;
    }

    @Override
    public List<EntityValue> getItemList() {
        Object object = this.getValue();
        if (object != null)
        {
            EntityDefinition listItemEntityDefinition = classEntityListProperty.getListItemEntityDefinition();
            if (listItemEntityDefinition instanceof ClassEntityDefinition)
            {
                ClassEntityDefinition itemEntityDefinition = (ClassEntityDefinition)listItemEntityDefinition;
                Collection<?> collection = (Collection<?>)object;
                List<EntityValue> list = new ArrayList<>();
                for(Object item : collection)
                {
                    ObjectEntityValue entityValue = new ObjectEntityValue(itemEntityDefinition, item, this.parent);
                    list.add(entityValue);
                }
                return list;
            }
        }
        return null;
    }

    @Override
    public void setValue(List<?> value) {
        if (this.parent.isNullValue()) {
            throw new IllegalArgumentException("Could set value for a null Object value entity");
        }
        else {
            Field field = this.classEntityListProperty.getField();
            if (!field.isAccessible()) {
                field.setAccessible(true);
            }
            try {
                field.set(this.parent.getValue(), value);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }
}